package com.xoa.service.party;


import com.xoa.dao.party.GridInfoMapper;
import com.xoa.dao.party.OrgDepartmentMapper;
import com.xoa.dao.party.SubjectActivityMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.GridInfo;
import com.xoa.model.party.OrgDepartmentWithBLOBs;
import com.xoa.model.party.SubjectActivity;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/16.
 */
@Service
public class SubjectActivityService {
    @Autowired
    SubjectActivityMapper subjectActivityMapper;
    @Autowired
    GridInfoMapper gridInfoMapper;

    @Autowired
    OrgDepartmentMapper orgDepartmentMapper;
    /**
    * @Author 程叶同
    * @Description  添加主题活动
    * @Date 13:24 2018/1/16
    */
    public BaseWrapper addSubjectActivity(SubjectActivity subjectActivity){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(subjectActivity!=null){
            int insert = subjectActivityMapper.insert(subjectActivity);
            if(insert>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("添加成功");
            }
        }
        return  baseWrapper;
    }


    /**
    * @Author 程叶同
    * @Description  删除
    * @Date 13:27 2018/1/16
    */
    public BaseWrapper deleteSubjectActivity(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null&&id!=0){
            int i = subjectActivityMapper.deleteByPrimaryKey(id);
            if(i>0){
                baseWrapper.setMsg("ok");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 修改
    * @Date 13:30 2018/1/16
    */
    public BaseWrapper updateSubjectActivity(SubjectActivity subjectActivity){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(subjectActivity!=null){
            int i = subjectActivityMapper.updateByPrimaryKey(subjectActivity);
            if(i>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("修改成功");
            }
        }
        return baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询单条信息
    * @Date 13:32 2018/1/16
    */
    public BaseWrapper getSubjectActivityById(HttpServletRequest request, Integer id){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=0&id!=null){
            SubjectActivity subjectActivity = subjectActivityMapper.selectByPrimaryKey(id);
            if(subjectActivity!=null){
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(subjectActivity.getAttachmentName(), subjectActivity.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                subjectActivity.setAttachmentList(attachments);
                String community = subjectActivity.getCommunity();
                if(community!=null){
                    GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(community));
                    if(gridInfo!=null){
                        if(Integer.parseInt(gridInfo.getUpperGrid())==0){
                            subjectActivity.setCommunityName(gridInfo.getGridName());
                        }else{
                            GridInfo gridInfo1 = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(gridInfo.getUpperGrid()));
                            subjectActivity.setCommunityName(gridInfo1.getGridName());
                        }
                    }else{
                        subjectActivity.setCommunityName("");
                    }

                }
                if(subjectActivity.getOrganize() !=null){
                    OrgDepartmentWithBLOBs orgDepartmentWithBLOBs = orgDepartmentMapper.selectByPrimaryKeyById(Integer.valueOf(subjectActivity.getOrganize()));
                    if(orgDepartmentWithBLOBs!=null&&orgDepartmentWithBLOBs.getOrgDeptName()!=null){
                        subjectActivity.setOrganize(orgDepartmentWithBLOBs.getOrgDeptName());
                    }
                }
                baseWrapper.setMsg("查询成功");
                baseWrapper.setData(subjectActivity);
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询
    * @Date 13:35 2018/1/16
    */
    public BaseWrapper getList(HttpServletRequest request, Integer page, Integer pageSize, String community, boolean useFlag, String startTime, String endTime, String subject, String organizeId){
        BaseWrapper baseWrapper=new BaseWrapper();

        List<OrgDepartmentWithBLOBs> orgDepartmentWithBLOBs = orgDepartmentMapper.queryDepIdList(null);
        List<Integer> list1=new ArrayList<Integer>();
        List<Integer> reOrg=null;
        if(organizeId!=null&&!"".equals(organizeId)){
            list1.add(Integer.parseInt(organizeId));
            reOrg= this.reOrgId(orgDepartmentWithBLOBs, Integer.parseInt(organizeId), list1);
        }


        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        map.put("startTime",startTime);
        map.put("endTIme",endTime);
        map.put("community",community);
        map.put("subject",subject);
        /*map.put("organizeId",organizeId);
        map1.put("organizeId",organizeId);*/
        map1.put("startTime",startTime);
        map1.put("endTIme",endTime);
        map1.put("community",community);
        map1.put("subject",subject);
        map1.put("id",reOrg);
        map.put("id",reOrg);
        List<SubjectActivity> subjectActivityList = subjectActivityMapper.selectList(map);
        int i = subjectActivityMapper.totalItems(map1);
        if(subjectActivityList!=null){
            for(SubjectActivity s:subjectActivityList){
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(s.getAttachmentName(), s.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                s.setAttachmentList(attachments);
                String community1 = s.getCommunity();
                if(community1!=null&&!"".equals(community1)){
                        GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(community1));
                        if(gridInfo!=null){
                            if(Integer.parseInt(gridInfo.getUpperGrid())==0){
                                s.setCommunityName(gridInfo.getGridName());
                            }else{
                                GridInfo gridInfo1 = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(gridInfo.getUpperGrid()));
                                s.setCommunityName(gridInfo1.getGridName());
                            }
                        }else{
                            s.setCommunityName("");
                        }
                }
            }
            baseWrapper.setData(subjectActivityList);
            baseWrapper.setTotle(i);
            baseWrapper.setMsg("ok");
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
        }
        return baseWrapper;
    }


    public List<Integer> reOrgId(List<OrgDepartmentWithBLOBs> orgDeplist, Integer orgdepId, List<Integer> list ){
        for (OrgDepartmentWithBLOBs orgDepartmentWithBLOBs : orgDeplist){
            if (orgdepId.equals(orgDepartmentWithBLOBs.getOrgDeptParent())){
                list.add(orgDepartmentWithBLOBs.getOrgDeptId());
                reOrgId(orgDeplist, orgDepartmentWithBLOBs.getOrgDeptId(), list);
            }
        }
        return list;
    }



}
